package com.jl.jlapp.mvp.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.jl.jlapp.R;
import com.jl.jlapp.eneity.MessageNoReadNumModel;
import com.jl.jlapp.eneity.OrderListModel;
import com.jl.jlapp.eneity.OrderListModel.ResultDataBean;
import com.jl.jlapp.mvp.activity.CenterActivity;
import com.jl.jlapp.mvp.activity.CouponCenterActivity;
import com.jl.jlapp.mvp.activity.CustomerServiceActivity;
import com.jl.jlapp.mvp.activity.InvoiceActivity;
import com.jl.jlapp.mvp.activity.LoginActivity;
import com.jl.jlapp.mvp.activity.MessageCenterActivity;
import com.jl.jlapp.mvp.activity.MyAddressActivity;
import com.jl.jlapp.mvp.activity.MyOrderListActivity;
import com.jl.jlapp.mvp.activity.SettingsActivity;
import com.jl.jlapp.mvp.activity.UserCouponActivity;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.nets.Transformer;
import com.jl.jlapp.utils.Tools;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.List;

public class Fragment_User extends Fragment implements OnClickListener {
    int afterSaleNum = 0;
    @BindView(R.id.after_sale_num)
    TextView afterSaleNumTv;
    @BindView(R.id.after_sale)
    LinearLayout after_sale_img;
    @BindView(R.id.all_order)
    LinearLayout all_order;
    @BindView(R.id.coupon)
    ImageView coupon;
    @BindView(R.id.coupon_center)
    ImageView coupon_center;
    Editor editor;
    @BindView(R.id.invoice)
    ImageView invoice;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.logout)
    TextView logout;
    @BindView(R.id.message_icon)
    ImageView message_icon;
    @BindView(R.id.my_address)
    ImageView my_address;
    @BindView(R.id.my_setting)
    ImageView my_setting;
    @BindView(R.id.server_and_help)
    ImageView server_and_help;
    SharedPreferences sharedPreferences;
    int userId = -1;
    String userName = "";
    String userPhone = "";
    String userPicUrl = "";
    @BindView(R.id.user_img)
    ImageView user_img;
    int waitEvqaluateNum = 0;
    @BindView(R.id.wait_evqaluate_num)
    TextView waitEvqaluateNumTv;
    int waitPayNum = 0;
    @BindView(R.id.wait_pay_num)
    TextView waitPayNumTv;
    int waitReceiveNum = 0;
    @BindView(R.id.wait_receive_num)
    TextView waitReceiveNumTv;
    @BindView(R.id.wait_evqaluate)
    LinearLayout wait_evqaluate;
    @BindView(R.id.wait_pay)
    LinearLayout wait_pay;
    @BindView(R.id.wait_receive)
    LinearLayout wait_receive;

    public void getMessageNum(int paramInteger) {
        Net.get().getMessageNum(paramInteger)
                .compose(Transformer.<MessageNoReadNumModel>switchSchedulers())
                .subscribe(new CommonObserver<MessageNoReadNumModel>() {

                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(MessageNoReadNumModel messageNoReadNumModel) {

                    }
                });
    }

    public void getOrderList(int paramInteger) {
        Net.get().getOrderList(paramInteger)
                .compose(Transformer.<OrderListModel>switchSchedulers())
                .subscribe(new CommonObserver<OrderListModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(OrderListModel orderListModel) {

                    }
                });
    }

    public void getShouldShowDataListByOrderState(List<OrderListModel.ResultDataBean> paramList) {
        for (int i = 0;i<paramList.size();i++){
            if (((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 6) {
                this.afterSaleNum += 1;
            }
            if (((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 0) {
                this.waitPayNum += 1;
            } else if (((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 2) {
                this.waitReceiveNum += 1;
            } else if (((((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 3) || (((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 8) || (((OrderListModel.ResultDataBean) paramList.get(i)).getOrderState() == 10)) && (((OrderListModel.ResultDataBean) paramList.get(i)).getIsHasEvaluation() == 0)) {
                this.waitEvqaluateNum += 1;
            }
        }
        if (this.afterSaleNum > 0) {
            this.afterSaleNumTv.setVisibility(View.VISIBLE);
            this.afterSaleNumTv.setText(this.afterSaleNum + "");
        } else {
            this.afterSaleNumTv.setVisibility(View.GONE);
        }
        if (this.waitPayNum > 0) {
            this.waitPayNumTv.setVisibility(View.VISIBLE);
            this.waitPayNumTv.setText(this.waitPayNum + "");
        } else {
            this.waitPayNumTv.setVisibility(View.GONE);
        }
        if (this.waitReceiveNum > 0) {
            this.waitReceiveNumTv.setVisibility(View.VISIBLE);
            this.waitReceiveNumTv.setText(this.waitReceiveNum + "");
        } else {
            this.waitReceiveNumTv.setVisibility(View.GONE);
        }

        if (this.waitEvqaluateNum > 0) {
            this.waitEvqaluateNumTv.setVisibility(View.VISIBLE);
            this.waitEvqaluateNumTv.setText(this.waitEvqaluateNum + "");
        } else {
            this.waitEvqaluateNumTv.setVisibility(View.GONE);
        }
    }

    public void init() {
        setOnClickEvent();
    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.user_img:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getContext(), SettingsActivity.class));
                break;
            case R.id.login:
                if (this.userId > 0) {
                    startActivity(new Intent(getContext(), SettingsActivity.class));
                    return;
                }
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.logout:
                if (this.sharedPreferences == null) {
                    this.sharedPreferences = getContext().getSharedPreferences("JLAppData", 0);
                    this.editor = this.sharedPreferences.edit();
                }
                this.editor.clear();
                this.editor.commit();
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.message_icon:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;
            case R.id.my_address:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getActivity(), MyAddressActivity.class));
                break;
            case R.id.invoice:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getActivity(), InvoiceActivity.class));
                break;
            case R.id.coupon:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getContext(), UserCouponActivity.class));
                break;
            case R.id.server_and_help:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getContext(), CustomerServiceActivity.class));
                break;
            case R.id.my_setting:
//                if (this.userId == -1) {
//                    startActivity(new Intent(getActivity(), LoginActivity.class));
//                    return;
//                }
                startActivity(new Intent(getContext(), SettingsActivity.class));
                break;
            case R.id.coupon_center:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getActivity(), CouponCenterActivity.class));
                break;
            case R.id.wait_pay:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent = new Intent(getContext(), MyOrderListActivity.class);
                intent.putExtra("orderState", 0);
                startActivity(intent);
                return;
            case R.id.wait_receive:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent1 = new Intent(getContext(), MyOrderListActivity.class);
                intent1.putExtra("orderState", 2);
                startActivity(intent1);
                break;
            case R.id.wait_evqaluate:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                startActivity(new Intent(getContext(), CenterActivity.class));
                break;
            case R.id.after_sale:
                if (this.userId == -1) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent2 = new Intent(getContext(), MyOrderListActivity.class);
                intent2.putExtra("orderState", 6);
                startActivity(intent2);
                break;
        }
    }

    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
        View view = paramLayoutInflater.inflate(R.layout.fragment_user, null, false);
        ButterKnife.bind(this, view);
        if (this.sharedPreferences == null) {
            this.sharedPreferences = getContext().getSharedPreferences("JLAppData", 0);
            this.editor = this.sharedPreferences.edit();
        }
        this.userId = this.sharedPreferences.getInt("userId", -1);
        this.userPicUrl = this.sharedPreferences.getString("userPicUrl", "");
        this.userPhone = this.sharedPreferences.getString("mobile", "");
        this.userName = this.sharedPreferences.getString("userName", "");
        init();
        return view;
    }

    public void onResume() {
        super.onResume();
        if (this.sharedPreferences == null) {
            this.sharedPreferences = getContext().getSharedPreferences("JLAppData", 0);
            this.editor = this.sharedPreferences.edit();
        }
        this.userId = this.sharedPreferences.getInt("userId", -1);
        this.userPicUrl = this.sharedPreferences.getString("userPicUrl", "");
        this.userPhone = this.sharedPreferences.getString("mobile", "");
        this.userName = this.sharedPreferences.getString("userName", "");
        if (this.userId != -1) {
            this.login.setText(this.userName);
            if (this.userPicUrl == "") {
                Glide.with(getActivity()).load(R.drawable.img_person_defult).into(this.user_img);
            }
            getMessageNum(this.userId);
            getOrderList(this.userId);
            Tools.setCircleIcon("http://117.158.178.202:8001/JLMIS/" + this.userPicUrl, this.user_img);
        } else {
            this.login.setText("登录/注册");
            this.login.setClickable(true);
            Glide.with(getActivity()).load(R.drawable.img_person_defult).into(this.user_img);
            this.message_icon.setImageResource(R.drawable.icon_message);
            this.afterSaleNum = 0;
            this.afterSaleNumTv.setVisibility(View.GONE);
            this.waitPayNum = 0;
            this.waitPayNumTv.setVisibility(View.GONE);
            this.waitReceiveNum = 0;
            this.waitReceiveNumTv.setVisibility(View.GONE);
            this.waitEvqaluateNum = 0;
            this.waitEvqaluateNumTv.setVisibility(View.GONE);
        }
    }

    public void setOnClickEvent() {
        this.user_img.setOnClickListener(this);
        this.login.setOnClickListener(this);
        this.logout.setOnClickListener(this);
        this.message_icon.setOnClickListener(this);
        this.my_address.setOnClickListener(this);
        this.invoice.setOnClickListener(this);
        this.coupon.setOnClickListener(this);
        this.server_and_help.setOnClickListener(this);
        this.my_setting.setOnClickListener(this);
        this.coupon_center.setOnClickListener(this);
        this.wait_pay.setOnClickListener(this);
        this.wait_receive.setOnClickListener(this);
        this.wait_evqaluate.setOnClickListener(this);
        this.after_sale_img.setOnClickListener(this);
        this.all_order.setOnClickListener(this);
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/fragment/Fragment_User.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */